from django.shortcuts import render_to_response, get_object_or_404
import copy
from django.contrib.auth.decorators import login_required
from sirogi.documents.models import Document
from sirogi.comments.models import Comment
from django.template import RequestContext

@login_required
def index(request):

	doc_num = ((request.META['PATH_INFO']).split('/'))[-2:][0]
	doc = Document.objects.get(numDoc = doc_num)
	st = doc.state
	stTxt = st.Desc

	try:
		request.POST['adding_comment']
		adding = True
	except:
		adding = False
	
	if adding:
		comment = Comment(document_id = doc.pk, author_id = request.user.pk, subject = request.POST['add_subject'], comment = request.POST['comment'])
		comment.save()

	results = Comment.objects.filter(document__numDoc = doc_num)
	
	is_colaborator = request.user in doc.colaborators.all()
	
	return render_to_response('comments/index.html', {'results': results, 'is_colaborator':is_colaborator, 'document':doc, 'st': stTxt},RequestContext(request))

